package com.joeyzh.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.joey.ui.general.BaseNoBarFragment;

import java.util.HashMap;

/**
 * Created by Joey on 2018/7/19.
 * 搜索的Filter的一个抽象封装
 */

public abstract class BaseFilterFragment extends BaseNoBarFragment {

    private FilterResultInterface resultInterface;

    // 用于存放扫描的键值对
    protected HashMap<String, String> filterResult = new HashMap<>();

    protected HashMap<Integer, String> searchKeys = new HashMap<>();
    // 存放基础字段列表
    protected HashMap<String, Object> baseMap = new HashMap<>();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initSearchInfo();
    }

    public void setResultInterface(FilterResultInterface resultInterface) {
        this.resultInterface = resultInterface;
    }

    public void search(View view) {
        if (!checkInfo()) {
            return;
        }
        if (resultInterface != null) {
            resultInterface.filterResult(filterResult,baseMap);
        }
    }

    /**
     * 将键值对与搜索条件绑定
     *
     * @param resId
     * @param key
     */
    public void bindSearchKeys(int resId, String key) {
        searchKeys.put(resId, key);
    }

    /**
     * 将键值对与搜索条件，绑定
     * @param id
     * @param key
     */
    public void bindSearchKeysRes(int id, String key) {
        bindSearchKeys(id, key);
        addResult(id, "");
    }

    public void clear() {
        filterResult.clear();
    }

    public void addResult(int resId, String value) {
        String key = searchKeys.get(resId);
        if (key == null) {
            return;
        }
        filterResult.put(key, value);
    }

    public boolean hasResult(int resId) {
        String key = searchKeys.get(resId);
        if (key == null) {
            return false;
        }
        String value = filterResult.get(key);
        if (value == null || value.isEmpty()) {
            return false;
        }
        return true;
    }

    public void addBaseInfo(String key, Object value) {
        baseMap.put(key, value);
    }

    public boolean checkInfo() {
        return true;
    }

    /**
     * 初始化搜索的基础数据
     */
    public abstract void initSearchInfo();


}
